package round;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;
import java.util.Stack;

public class RoundCount {
    private LinkedList<Integer> round;

    @Test
    public int initRound(int[] speeds) {
        int totalSpeed = 0;
        for (int i = 0; i < 8; i++) {
            totalSpeed += speeds[i];
        }
        int allRound = totalSpeed;
        round = new LinkedList<>();
        Random random = new Random();
        for (int index = 0; index < allRound; index++) {
            int rand = random.nextInt(totalSpeed) + 1;
            for (int i = 0; i < 8; i++) {
                rand -= speeds[i];
                if (rand <= 0 && speeds[i] != 0) {
                    round.add(i);
                    speeds[i]--;
                    totalSpeed--;
                    break;
                }
            }
        }
        return round.pollFirst();
    }

    public int nextTurn() {
        return round.pollFirst();
    }

    public int getLeftTurns() {
        return round.size();
    }

    public int[] getRound() {
        return round.stream().mapToInt(Integer::valueOf).toArray();
    }

    @Test
    public void test() {
        int[] speeds = {2, 0, 0, 0, 1, 0, 0, 0};
        initRound(speeds);
    }

    public int[] flash(int kill) {
        for (int i = 0; i < round.size(); i++) {
            if (round.get(i) == kill) {
                round.remove(i);
                i--;
            }
        }
        return round.stream().mapToInt(Integer::valueOf).toArray();
    }
}
